package com.pktechan.manage.service.impl;


import com.pktechan.manage.base.BaseMapper;
import com.pktechan.manage.base.BaseServiceImpl;
import com.pktechan.manage.dao.ProductChannelFeeMapper;
import com.pktechan.manage.entity.ProductChannelFee;
import com.pktechan.manage.enumeration.CommissionGenerationEnum;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.service.ProductChannelFeeService;
import com.pktechan.manage.vo.ProductVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("productChannelFeeService")
public class ProductChannelFeeServiceImpl extends BaseServiceImpl<ProductChannelFee> implements ProductChannelFeeService {

    private static final Logger logger = LoggerFactory.getLogger(ProductChannelFeeServiceImpl.class);

    @Autowired
    private ProductChannelFeeMapper productChannelFeeMapper;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<ProductChannelFee> getBaseMapper() {
        return productChannelFeeMapper;
    }

    @Override
    public List<ProductVO> getProductChannelFeeList(ProductVO productVO) {
        return productChannelFeeMapper.getProductChannelFeeList(productVO);
    }

    /**
     * 添加到产品营销渠道费
     *
     * @param productChannelFee
     */
    @Override
    public void saveProductChannelFee(ProductChannelFee productChannelFee) {
        //是否可以添加校验
        logger.debug("根据productId=" + productChannelFee.getProductId() + "和commissionGeneration=" + productChannelFee.getCommissionGeneration() + "查询有效产品营销渠道费");
        ProductChannelFee byProductId = productChannelFeeMapper.getProductChannelFeeByProductId(productChannelFee);
        if (byProductId == null) {
            logger.debug("保存产品营销渠道费");
            productChannelFeeMapper.save(productChannelFee);
        } else {
            throw new CustomException("产品id=" + productChannelFee.getProductId() + "的渠道代数:" + CommissionGenerationEnum.getNameByCode(productChannelFee.getCommissionGeneration()) + "，业务种类：" +productChannelFee.getBusinessType()+
                    "，已有状态为：有效的，请勿重复添加。如需添加请把状态有效的改为无效。");
        }
    }

    /**
     * 修改产品营销渠道费
     *
     * @param productChannelFee
     */
    @Override
    public void updateProductChannelFee(ProductChannelFee productChannelFee) {
        logger.debug("根据productId=" + productChannelFee.getProductId() + "和commissionGeneration=" + productChannelFee.getCommissionGeneration() + "查询有效产品营销渠道费");
        ProductChannelFee byProductId = productChannelFeeMapper.getProductChannelFeeByProductId(productChannelFee);
        logger.debug("根据id="+productChannelFee.getId()+"查询产品营销渠道费信息");
        ProductChannelFee byId = (ProductChannelFee) productChannelFeeMapper.getById(productChannelFee.getId());
        if (byProductId != null && !byProductId.getCommissionGeneration().equals(byId.getCommissionGeneration())) {
            throw new CustomException("产品id=" + productChannelFee.getProductId() + "的渠道代数:" + CommissionGenerationEnum.getNameByCode(productChannelFee.getCommissionGeneration()) + "" +
                    "已有状态为：有效的，请勿重复添加。如需添加请把状态有效的改为无效。");
        }
        logger.debug("修改产品营销渠道费");
        productChannelFeeMapper.update(productChannelFee);
    }

    /**
     * 根据id查询产品营销渠道费
     *
     * @param channelFeeId
     * @return
     */
    @Override
    public ProductVO getProductChannelFeeById(Integer channelFeeId) {
        return productChannelFeeMapper.getProductChannelFeeById(channelFeeId);
    }
}
